package com.itheima.health.service.impl;

import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 批量添加
     * @param list
     */
    @Transactional
    @Override
    public void add(List<OrderSetting> list) {
        if(!CollectionUtils.isEmpty(list)){
            for (OrderSetting orderSetting : list) {
                //改变时间格式
                String orderDate = getOrderDate(orderSetting.getOrderDate());
                //检查此数据是否存在
                long count = orderSettingDao.findCountByOrderDate(orderDate);
                if(count > 0){
                    //已经存在，执行更新操作
                    orderSettingDao.editNumberByOrderDate(
                            orderSetting.getNumber(),
                            orderDate);
                }else{
                    //不存在，执行添加操作
                    orderSettingDao.add(
                            orderDate,
                            orderSetting.getNumber(),
                            orderSetting.getReservations());
                }
            }
        }
    }

    /**
     * 根据日期查询预约设置数据
     * @param year
     * @param month
     * @return
     */
    @Override
    public List<Map<Object,Object>> getOrderSettingByMonth(Integer year, Integer month) {//2019-3
        //获取每月的第一天
        String  begin = getFirstDayOfMonth(year, month - 1);
        //获取每月的最后一天
        String end = getLastDayOfMonth(year,month - 1);
        Map<Object,Object> map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        List<Map<Object,Object>> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map<Object,Object> orderSettingMap = new HashMap<>();
            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());//获得日期（几号）
            orderSettingMap.put("number",orderSetting.getNumber());//可预约人数
            orderSettingMap.put("reservations",orderSetting.getReservations());//已预约人数
            data.add(orderSettingMap);
        }
        return data;
    }

    //获取每月的第一天
    private String getFirstDayOfMonth(Integer year, Integer month) {
        Calendar cal = Calendar.getInstance();
        //清除可能存在的缓存
        cal.clear();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month);
        //获取某月最小天数
        int firstDay = cal.getMinimum(Calendar.DATE);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH,firstDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }

    //获取每月的最后一天
    private String getLastDayOfMonth(Integer year, Integer month) {
        Calendar cal = Calendar.getInstance();
        //清除可能存在的缓存
        cal.clear();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DATE);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }

    /**
     * 根据指定日期修改可预约人数
     * @param orderSetting
     */
    @Transactional
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //改变时间格式
        String orderDate = getOrderDate(orderSetting.getOrderDate());
        long count = orderSettingDao.findCountByOrderDate(orderDate);
        if(count > 0){
            orderSettingDao.editNumberByOrderDate(
                    orderSetting.getNumber(),
                    orderDate);
        }else{
            orderSettingDao.add(
                    orderDate,
                    orderSetting.getNumber(),
                    orderSetting.getReservations());
        }
    }

    /**
     * 改变时间格式
     * @param orderDate
     * @return
     */
    private String getOrderDate(Date orderDate) {
        return new SimpleDateFormat("yyyy-MM-dd").format(orderDate);
    }
}
